package com.siysoft.util;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.map.ObjectMapper;




public class StrUtil {

	/**
	 * 将ISO8859_1编码的字符串转码为GBK编码
	 * 
	 * @param s
	 *            源串
	 * @return 结果串
	 */
	public static String UNI2GBK(String s) {
		String temp = "";
		try {
			if (s != null) {
				temp = s;
				temp = new String(s.getBytes(), "GBK");
			}
		} catch (Exception e) {
		} finally {
			return temp;
		}
	}

	/**
	 * 将GBK编码的字符串转码为ISO8859_1编码
	 * 
	 * @param s
	 *            源串
	 * @return 结果串
	 */
	public static String GBK2UNI(String s) {
		String temp = "";
		try {
			if (s != null) {
				temp = s;
				temp = new String(s.getBytes("GBK"), "ISO8859_1");
			}
		} catch (Exception e) {

		} finally {
			return temp;
		}
	}

	public static String GBK2UTF_8(String s) {
		String temp = "";
		try {
			if (s != null) {
				temp = s;
				temp = new String(s.getBytes("GBK"), "UTF_8");
			}
		} catch (Exception e) {

		} finally {
			return temp;
		}
	}

	/**
	 * 
	 * <pre>
	 * 字符串替换函数
	 * </pre>
	 * 
	 * @param from
	 *            要替换的字符
	 * @param to
	 *            要替换成的目标字符
	 * @param source
	 *            要替换的字符串
	 * @return 替换后的字符串
	 * @add-time: 2007-01-27
	 * @add-athname: guke
	 */
	public static String replace(String strSource, String strFrom, String strTo) {
		if (strSource == null) {
			return null;
		}
		int i = 0;
		if ((i = strSource.indexOf(strFrom, i)) >= 0) {
			char[] cSrc = strSource.toCharArray();
			char[] cTo = strTo.toCharArray();
			int len = strFrom.length();
			StringBuffer buf = new StringBuffer(cSrc.length);
			buf.append(cSrc, 0, i).append(cTo);
			i += len;
			int j = i;
			while ((i = strSource.indexOf(strFrom, i)) > 0) {
				buf.append(cSrc, j, i - j).append(cTo);
				i += len;
				j = i;
			}
			buf.append(cSrc, j, cSrc.length - j);
			return buf.toString();
		}
		return strSource;
	}

	/**
	 * <pre>
	 * 取得字符串的编码
	 * </pre>
	 * 
	 * @param stream
	 *            要变换的字符串
	 * @return 字符串的编码(１６进制)
	 * @add-time: 2007-02-01
	 * @add-athname: guke
	 */
	public static String toStrCode(String stream) {
		if (stream == null || stream.length() == 0) {
			return stream;
		}

		byte[] bytes = null;
		try {
			bytes = stream.getBytes("gb2312");
		} catch (Exception e) {
			bytes = stream.getBytes();
		}

		StringBuffer buffer = new StringBuffer();
		if (bytes != null) {
			for (int lopcnt = 0; lopcnt < bytes.length; lopcnt++) {
				buffer.append(Integer.toHexString(bytes[lopcnt] & 0x0000FF));
			}
		}

		return buffer.toString().toUpperCase();
	}

	/**
	 * <pre>
	 * 从字符串的编码转换成字符串
	 * </pre>
	 * 
	 * @param stream
	 *            字符串的编码(１６进制)
	 * @return 变换成的字符串
	 * @add-time: 2007-02-01
	 * @add-athname: guke
	 */
	public static int hexToDec(String stream) {
		if (stream == null || stream.length() == 0) {
			return Integer.MIN_VALUE;
		}

		try {
			return Integer.parseInt(stream, 16);
		} catch (Exception e) {
			return Integer.MIN_VALUE;
		}
	}


	/**
	 * 取cookie中的cooknames所指定的变量值
	 * 
	 * @param request
	 * @param cooknames
	 * @return
	 */
	public static String getCookiesValues(HttpServletRequest request,
			String cooknames) {
		// 从cookie读取cooknames值;
		String returnValues = "";
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (int i = 0; i < cookies.length; i++) {
				Cookie cookie = cookies[i];
				String cookieName = cookie.getName();
				if (cookieName.equals(cooknames)) {
					returnValues = Encoder.decodeURL(cookie.getValue());
				}
			}
		}
		return returnValues;
	}

	/**
	 * 判断字符串是否为空
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isBlank(String str) {
		if (str == null || str.equals("")) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 判断是否为数字
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNumber(String str) {
		if (str == null || "".equals(str.trim())) {
			return false;
		}

		if (str.matches("\\d+$")) {
			return true;
		} else {
			return false;
		}

	}

	public static boolean isBlank(Serializable str) {
		if (str == null) {
			return true;
		} else {
			if (((String) str).equals("")) {
				return true;
			}
			return false;
		}
	}

	public static boolean isBlank(Object obj) {
		if (obj == null) {
			return true;
		} else {
			if (((String) obj).equals("")) {
				return true;
			}
			return false;
		}
	}

	/**
	 * 检查一个字符串数组中是否含有指定的字符
	 * 
	 * @param str
	 * @param source
	 * @return 0:包含;1:检查出现异常;-1:不包含
	 */
	public static int testStringInString(String str, String[] source) {
		try {
			for (int i = 0; i < source.length; i++) {
				if (str.equals(source[i]))
					return 0;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return 1;
		}
		return -1;
	}

	/**
	 * 检查一个字符串中是否含有指定的字符
	 * 
	 * @param str:要查找的字符
	 * @param source:
	 * @param splits:
	 * @return
	 */
	public static int testStringInString(String str, String source,
			String splits) {
		String[] tempS = source.split(splits);
		try {
			for (int i = 0; i < tempS.length; i++) {
				if (str.equals(tempS[i]))
					return 0;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return 1;
		}
		return -1;
	}

	public static String setStrings(String str, int len) {
		int strlen = 0;
		String s = "";

		for (int i = 0; i < str.length(); i++) {
			if (str.charAt(i) > 128)
				strlen += 2;
			else
				strlen++;
			s += str.charAt(i);
			if (strlen >= len)
				return s + "...";
		}
		return s;
	}

	public static String setStrings2(String str, int len) {
		int strlen = 0;
		String s = "";

		for (int i = 0; i < str.length(); i++) {
			if (str.charAt(i) > 128)
				strlen += 2;
			else
				strlen++;
			s += str.charAt(i);
			if (strlen >= len)
				return s;
		}
		return s;
	}

	public static String setStrings3(String str, int len) {
		int strlen = 0;
		String s = "";

		for (int i = 0; i < str.length(); i++) {
			if (str.charAt(i) > 128)
				strlen += 2;
			else
				strlen++;
			s += str.charAt(i);
			if (strlen > len)
				return s + "…";
		}
		return s;
	}

	public static String null2String(String str) {
		if (str == null)
			str = "";
		return str;
	}

	public static int string2Int(String str) {
		if (str == null || str.equals(""))
			return 0;
		try {
			return Integer.parseInt(str);
		} catch (Exception e) {
			return 0;
		}

	}

	public static Long string2Long(String str) {
		if (str == null || str.equals(""))
			return new Long(0);
		try {
			return Long.parseLong(str);
		} catch (Exception e) {
			return new Long(0);
		}

	}
	
	public static String Long2String2(Long num) {
		if (num == null)
			return "";
		try {
			return num.toString();
		} catch (Exception e) {
			return "";
		}

	}

	public static Double string2Double(String str) {
		if (str == null || str.equals(""))
			return new Double(0);
		try {
			return new Double(Double.parseDouble(str));
		} catch (Exception e) {
			return new Double(0);
		}

	}
	
	public static Float string2Float(String str) {
		if (str == null || str.equals(""))
			return new Float(0);
		try {
			return new Float(Float.parseFloat(str));
		} catch (Exception e) {
			return new Float(0);
		}

	}

	public static String citytoken(String type, String str) {
		if (isBlank(str)) {
			return "";
		} else {
			StringTokenizer token = new StringTokenizer(str, "|");
			String province = "";
			String city = "";
			int i = 0;
			while (token.hasMoreTokens()) {

				if (i == 0)
					province = token.nextToken();
				else
					city = token.nextToken();
				i++;
			}
			if (type.equals("province"))
				return province;
			else if (type.equals("city"))
				return city;
			else
				return "";
		}

	}
	
	public static List<Map<String,Object>> Json2List(String jsonData) throws Exception{
		JsonGenerator jsonGenerator = null;
	    ObjectMapper objectMapper = null;
	    objectMapper = new ObjectMapper();
	    List<Map<String,Object>> list = null;
        try {
        	 list = objectMapper.readValue(jsonData, List.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
}
	
	
	
}
